Package client.teavm

Klasse ImportedApiSAB

java.lang.Object
client.teavm.ImportedApiSAB
Alle implementierten Schnittstellen:
IImportedApi

public final class ImportedApiSAB extends Object implements IImportedApi
SharedArrayBuffer (SAB)-based implementation of ImportedApi.

This class enables high-performance, zero-copy data exchange between a TeaVM Web Worker and the browser main thread. Large buffers for audio, video, and oscilloscope data are shared via ByteBuffer backed by SABs, and synchronization is achieved using atomic operations on a small control buffer.

Buffers

  • Audio samples: Written by the worker and read by the main thread.
  • Video pixels: Written by the worker and read by the main thread.
  • Oscilloscope gauges: Each chip has a map of shared buffers for waveform and envelope data.

Control Buffer

A small Int32Array stores atomic flags for synchronization:

 jsControlBuffer[0] → SAMPLE_FLAG_INDEX (audio ready)
 jsControlBuffer[1] → FRAME_FLAG_INDEX  (video ready)
 jsControlBuffer[2+] → OSC0_FLAG_INDEX + sidNum (oscilloscope ready per SID)
 

Flag values:

Synchronization Protocol

  1. Worker writes to a buffer and sets the corresponding flag to ImportedApiSAB.CONTROL.FLAGS.READY.
  2. Main thread processes the buffer, resets the flag to ImportedApiSAB.CONTROL.FLAGS.NOT_READY, and notifies the worker.
  3. Worker waits using Atomics.wait(org.teavm.jso.typedarrays.Int32Array, int, int) until the flag is cleared, then continues.

This approach ensures efficient handshaking for real-time audio/video and oscilloscope data without copying large arrays.

Note: Currently, SABs are used for audio samples, video frames, and oscilloscope gauges only.